package com.hifar.test.syncData.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hifar.mybatisplus.PublicMapper;

import com.hifar.sys.DbUtils;
import com.hifar.test.syncData.pojo.HfEnvSyncTableConfig;
import com.hifar.test.syncData.service.ICollectDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhuWeiWei
 * @description 采集数据查询服务
 * @date 2025/4/17 11:46
 */
@Service
public class CollectDataSourceServiceImpl implements ICollectDataSourceService {

    @Autowired
    private PublicMapper publicMapper;
    @Autowired
    private DbUtils dbUtils;

    @Override
    public boolean tableIsExist(String tableName) {
        return dbUtils.tableIsExist(tableName);
    }

    @Override
    public List<Map<String, Object>> listTableData(HfEnvSyncTableConfig syncTableConfig) {
        String tableName = syncTableConfig.getTableName();
        boolean exist = this.tableIsExist(tableName);
        if (!exist) {
            return new ArrayList<>();
        }
        Date lastSyncTime = syncTableConfig.getLastSyncTime();
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt(Objects.nonNull(lastSyncTime), "create_time", lastSyncTime);
        String querySql = "select * from " + tableName;
        return publicMapper.selectList(querySql, queryWrapper);
    }
}
